/**
 *  Copyright (C) 2008 Richard Hall
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the 
 *  GNU General Public License as published by the Free Software Foundation; either version 3 of the 
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with this program; if not, 
 *  see <http://www.gnu.org/licenses>.
 *
 *  Additional permission under GNU GPL version 3 section 7
 *
 *  If you modify this Program, or any covered work, by linking or combining it with [name of library] 
 *  (or a modified version of that library), containing parts covered by the terms of 
 *  [name of library's license], the licensors of this Program grant you additional permission to 
 *  convey the resulting work. {Corresponding Source for a non-source form of such a combination shall 
 *  include the source code for the parts of [name of library] used as well as that of the 
 *  covered work.}
 *
 * 
 */

package com.gpsmedia.audio;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.decoder.JavaLayerException;

/**
 * This class is responsible for playing an MP3 file. It has a simple 'start'
 * and 'stop' mechanism, nothing more. Makes use of functionality provided by JavaZoom 
 * 
 * http://www.javazoom.net/javalayer/docs/docs1.0/index.html
 * 
 * Based on an example found here: http://www.informit.com/guides/content.aspx?g=java&seqNum=290
 * 
 * @author Richard Hall
 */
public class AudioPlayer {

    private String mp3File;
    private javazoom.jl.player.Player player;

    /**
	 * This constructor takes the name of the file to played.
	 * 
	 * @param filename
	 *            The name of the file to be played.
	 */
    public AudioPlayer(String filename) {
        mp3File = filename;
    }

    /** Stops the object from playing the MP3 file. */
    public void stopMusic() {
        if (player != null) {
            player.close();
        }
    }

    /**
	 * Returns the name of the MP3 file.
	 * 
	 * @return The name of the file.
	 */
    public String getFilename() {
        return mp3File;
    }

    /**
	 * Inner class to make use of the Thread functionality.
	 * 
	 * @author Richard
	 * @see Thread
	 */
    private class AudioThread extends Thread {

        public void run() {
            try {
                player.play();
            } catch (JavaLayerException ex) {
                Logger.getLogger(AudioPlayer.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    /** Play the MP3 file. */
    public void playAudio() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(mp3File);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new javazoom.jl.player.Player(bis);


            // run in new thread to play in background
            AudioThread playThread = new AudioThread();
            playThread.start();
        } catch (JavaLayerException ex) {
            Logger.getLogger(AudioPlayer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AudioPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}